package com.zw.yzk.kabu.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.model.PublishImageModel;
import com.zw.yzk.kabu.utils.PhoneUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by zhanwei on 2016/12/28.
 */

public class CosImageListAdapter extends RecyclerView.Adapter<CosImageListAdapter.Holder> {

    private List<PublishImageModel> modelList;
    private Context context;

    public CosImageListAdapter(List<PublishImageModel> list) {
        modelList = new ArrayList<>();
        if (list != null) {
            modelList.addAll(list);
        }
    }

    public void refresh(List<PublishImageModel> list) {
        modelList.clear();
        if (list != null) {
            modelList.addAll(list);
            notifyDataSetChanged();
        }
    }

    public void add(List<PublishImageModel> list) {
        if (list != null) {
            int startPosition = modelList.size();
            modelList.addAll(list);
            notifyItemRangeInserted(startPosition, list.size());
        }
    }

    public ArrayList<PublishImageModel> getImageList() {
        ArrayList<PublishImageModel> result = new ArrayList<>();
        result.addAll(modelList);
        return result;
    }


    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (context == null) {
            context = parent.getContext();
        }
        View view = LayoutInflater.from(context).inflate(R.layout.recycler_item_edit_cos_image_list, parent, false);

        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(final Holder holder, int position) {
        PublishImageModel model = modelList.get(position);

        Glide.with(context)
                .load(model.getContentPath())
                .centerCrop()
                .error(R.drawable.im_default)
                .override(PhoneUtils.getScreenWidth(context), PhoneUtils.getScreenWidth(context))
                .into(holder.content);
        holder.remark.setText(model.getRemark());

        holder.remove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                modelList.remove(position);
                notifyItemRemoved(position);
            }
        });

        holder.remark.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                modelList.get(holder.getAdapterPosition()).setRemark(s.toString());
            }
        });
    }

    @Override
    public int getItemCount() {
        return modelList.size();
    }

    public static class Holder extends RecyclerView.ViewHolder {
        @Bind(R.id.content)
        ImageView content;
        @Bind(R.id.remove)
        ImageView remove;
        @Bind(R.id.message)
        EditText remark;

        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
